package com.agger.harmonyosthemeseditor.userinterface;

import com.agger.harmonyosthemeseditor.ResourceTable;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.service.DisplayAttributes;
import ohos.agp.window.service.DisplayManager;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;

public class RadioDialog
{
    private Context mContext;
    private String mTitle,mCancel;
    private String[] mItems;
    private int mIndex,itemValue;
    private boolean mOutsideTouchClosable;
    private CommonDialog sDialog;
    private StackLayout[] ItemLayouts,RadioButtons;
    private DirectionalLayout[] StatusPressedItems;
    private StackLayout[] StatusNormalItems;
    private DirectionalLayout[] ItemTextLayouts,RadioLayouts;
    private Text[] ItemTexts;
    private Image[] RadioEmpty,RadioChecked;
    private CommonDialog.DestroyedListener mDestroyedListener;
    private DisplayAttributes displayAttributes;
    public boolean isDestroyed = false;

    public RadioDialog(Context context)
    {
        mContext = context;
    }

    public RadioDialog setTitle(String title)
    {
        mTitle = title;
        return this;
    }

    public RadioDialog setContent(String[] items,int index)
    {
        mItems = items;
        mIndex = index;
        itemValue = index;
        return this;
    }

    public RadioDialog setCancelText(String cancelText)
    {
        mCancel = cancelText;
        return this;
    }

    public RadioDialog setOutsideTouchClosable(boolean closable)
    {
        mOutsideTouchClosable = closable;
        return this;
    }

    public void setDestroyedListener(CommonDialog.DestroyedListener destroyedListener)
    {
        mDestroyedListener = destroyedListener;
    }

    public void show()
    {
        float sDim = (ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_color_mode,-1)==-16777216)?0.4f:0.2f;
        displayAttributes = DisplayManager.getInstance().getDefaultDisplay(mContext).get().getAttributes();
        int maxHeight = displayAttributes.height - (int)((47f*displayAttributes.scalDensity)+0.5f);
        sDialog = new CommonDialog(mContext);
        int DialogWidth;
        if((((float)displayAttributes.height)/((float)displayAttributes.width))>1.48f)
        {
            DialogWidth = displayAttributes.width - (int)((24f*displayAttributes.scalDensity)+0.5f);
            sDialog.setAlignment(LayoutAlignment.BOTTOM);
            sDialog.setOffset(0,(int)((16f*displayAttributes.scalDensity)+0.5f));
        }
        else
        {
            if((((float)displayAttributes.width)/((float)displayAttributes.height))<1.48f) DialogWidth = displayAttributes.width - (int)((111f*displayAttributes.scalDensity)+0.5f);
            else DialogWidth = (int)((displayAttributes.width*0.4775f)+0.5f);
            if(DialogWidth < (int)((320f*displayAttributes.scalDensity)+0.5f))
                DialogWidth = (int)((320f*displayAttributes.scalDensity)+0.5f);
            else if(DialogWidth > (int)((480f*displayAttributes.scalDensity)+0.5f))
                DialogWidth = (int)((480f*displayAttributes.scalDensity)+0.5f);
            sDialog.setAlignment(LayoutAlignment.CENTER);
            sDialog.setOffset(0,0);
        }
        sDialog.setTransparent(true);
        Component dialogLayout = initDialog();
        sDialog.setContentCustomComponent(dialogLayout);
        sDialog.setSize(DialogWidth, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        sDialog.setAutoClosable(mOutsideTouchClosable);
        sDialog.setMovable(false);
        sDialog.setDestroyedListener(mDestroyedListener);
        sDialog.show();
        Window sWindow = sDialog.getWindow();
        WindowManager.LayoutConfig layoutConfig =sWindow.getLayoutConfig().get();
        layoutConfig.dim = sDim;
        sWindow.setLayoutConfig(layoutConfig);
        int dialogContentHeight = dialogLayout.getHeight();
        if(dialogContentHeight>maxHeight)
        {
            Component contentScroller = dialogLayout.findComponentById(ResourceTable.Id_content_scroller);
            int scrollerHeight = contentScroller.getHeight()-dialogContentHeight+maxHeight;
            CommonDialog oldDialog = sDialog;
            sDialog = new CommonDialog(mContext);
            if((((float)displayAttributes.height)/((float)displayAttributes.width))>1.48f)
            {
                sDialog.setAlignment(LayoutAlignment.BOTTOM);
                sDialog.setOffset(0,(int)((16f*displayAttributes.scalDensity)+0.5f));
            }
            else
            {
                sDialog.setAlignment(LayoutAlignment.CENTER);
                sDialog.setOffset(0,0);
            }
            sDialog.setTransparent(true);
            dialogLayout = initDialog();
            contentScroller = dialogLayout.findComponentById(ResourceTable.Id_content_scroller);
            contentScroller.setHeight(scrollerHeight);
            dialogLayout.setHeight(maxHeight);
            sDialog.setContentCustomComponent(dialogLayout);
            sDialog.setSize(DialogWidth, ComponentContainer.LayoutConfig.MATCH_CONTENT);
            sDialog.setAutoClosable(mOutsideTouchClosable);
            sDialog.setMovable(false);
            sDialog.setDestroyedListener(mDestroyedListener);
            sDialog.show();
            sWindow = sDialog.getWindow();
            layoutConfig =sWindow.getLayoutConfig().get();
            layoutConfig.dim = sDim;
            sWindow.setLayoutConfig(layoutConfig);
            oldDialog.hide();
            oldDialog.destroy();
        }
        isDestroyed = false;
        System.gc();
    }

    private Component initDialog()
    {
        Component dialogLayout = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_radio_dialog, null, false);
        Component radioDialogLayout = dialogLayout.findComponentById(ResourceTable.Id_RadioDialogLayout);
        radioDialogLayout.setBackground(new ShapeElement(){{setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_radio_dialog_bg,-1)));setCornerRadius((int)((24f*displayAttributes.scalDensity)+0.5f));}});
        ((Text)dialogLayout.findComponentById(ResourceTable.Id_title)).setText(mTitle);
        DirectionalLayout contentLayout = (DirectionalLayout)dialogLayout.findComponentById(ResourceTable.Id_content_layout);
        ItemLayouts = new StackLayout[mItems.length];
        StatusPressedItems = new DirectionalLayout[mItems.length];
        StatusNormalItems = new StackLayout[mItems.length];
        ItemTextLayouts = new DirectionalLayout[mItems.length];
        ItemTexts = new Text[mItems.length];
        RadioLayouts = new DirectionalLayout[mItems.length];
        RadioButtons = new StackLayout[mItems.length];
        RadioEmpty = new Image[mItems.length];
        RadioChecked = new Image[mItems.length];
        for(int i = 0;i < mItems.length;i++)
        {
            if(i>0)
            {
                DirectionalLayout Divider = new DirectionalLayout(mContext);
                Divider.setHeight(1);
                Divider.setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
                Divider.setMarginLeft((int)((24f*displayAttributes.scalDensity)+0.5f));
                Divider.setMarginRight((int)((24f*displayAttributes.scalDensity)+0.5f));
                Divider.setBackground(new ShapeElement() {{setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_radio_dialog_divider,-3355444)));}});
                contentLayout.addComponent(Divider);
            }
            ItemLayouts[i] = new StackLayout(mContext);
            ItemLayouts[i].setHeight(StackLayout.LayoutConfig.MATCH_CONTENT);
            ItemLayouts[i].setWidth(StackLayout.LayoutConfig.MATCH_PARENT);
            contentLayout.addComponent(ItemLayouts[i]);
            StatusPressedItems[i] = new DirectionalLayout(mContext);
            StatusPressedItems[i].setHeight(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            StatusPressedItems[i].setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            StatusPressedItems[i].setBackground(new ShapeElement() {{setRgbColor(RgbColor.fromArgbInt(ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_radio_dialog_item_pressed,-1644826)));}});
            StatusPressedItems[i].setVisibility(Component.INVISIBLE);
            ItemLayouts[i].addComponent(StatusPressedItems[i]);
            StatusNormalItems[i] = new StackLayout(mContext);
            StatusNormalItems[i].setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            StatusNormalItems[i].setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            ItemLayouts[i].addComponent(StatusNormalItems[i]);
            ItemTextLayouts[i] = new DirectionalLayout(mContext);
            ItemTextLayouts[i].setHeight(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            ItemTextLayouts[i].setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            ItemTextLayouts[i].setOrientation(Component.HORIZONTAL);
            ItemTextLayouts[i].setAlignment(LayoutAlignment.VERTICAL_CENTER);
            StatusNormalItems[i].addComponent(ItemTextLayouts[i]);
            ItemTexts[i] = new Text(mContext);
            ItemTexts[i].setHeight(DirectionalLayout.LayoutConfig.MATCH_CONTENT);
            ItemTexts[i].setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            ItemTexts[i].setMarginLeft((int)((24f*displayAttributes.scalDensity)+0.5f));
            ItemTexts[i].setMarginRight((int)((48f*displayAttributes.scalDensity)+0.5f));
            ItemTexts[i].setTextColor(new Color(ResourceTool.getColor(mContext,ResourceTable.Color_dawnlighti_radio_dialog_text,-15132391)));
            ItemTexts[i].setTextSize(AttrHelper.fp2px(16,mContext));
            ItemTexts[i].setMultipleLine(true);
            ItemTexts[i].setText(mItems[i]);
            ItemTextLayouts[i].addComponent(ItemTexts[i]);
            RadioLayouts[i] = new DirectionalLayout(mContext);
            RadioLayouts[i].setHeight(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            RadioLayouts[i].setWidth(DirectionalLayout.LayoutConfig.MATCH_PARENT);
            RadioLayouts[i].setAlignment(LayoutAlignment.RIGHT|LayoutAlignment.VERTICAL_CENTER);
            StatusNormalItems[i].addComponent(RadioLayouts[i]);
            RadioButtons[i] = new StackLayout(mContext);
            RadioButtons[i].setHeight((int)((48f*displayAttributes.scalDensity)+0.5f));
            RadioButtons[i].setWidth((int)((48f*displayAttributes.scalDensity)+0.5f));
            RadioButtons[i].setMarginRight((int)((16f*displayAttributes.scalDensity)+0.5f));
            RadioLayouts[i].addComponent(RadioButtons[i]);
            StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig();
            layoutConfig.alignment =LayoutAlignment.CENTER;
            RadioEmpty[i] = new Image(mContext);
            RadioEmpty[i].setLayoutConfig(layoutConfig);
            RadioEmpty[i].setImageElement(new VectorElement(mContext,ResourceTable.Graphic_background_raidobutton_empty));
            RadioEmpty[i].setHeight((int)((20f*displayAttributes.scalDensity)+0.5f));
            RadioEmpty[i].setWidth((int)((20f*displayAttributes.scalDensity)+0.5f));
            RadioButtons[i].addComponent(RadioEmpty[i]);
            RadioChecked[i] = new Image(mContext);
            RadioChecked[i].setLayoutConfig(layoutConfig);
            RadioChecked[i].setImageElement(new VectorElement(mContext,ResourceTable.Graphic_background_raidobutton_checked));
            RadioChecked[i].setHeight((int)((20f*displayAttributes.scalDensity)+0.5f));
            RadioChecked[i].setWidth((int)((20f*displayAttributes.scalDensity)+0.5f));
            RadioButtons[i].addComponent(RadioChecked[i]);
            if(i==mIndex)
            {
                RadioEmpty[i].setVisibility(Component.INVISIBLE);
                RadioChecked[i].setVisibility(Component.VISIBLE);
            }else
            {
                RadioEmpty[i].setVisibility(Component.VISIBLE);
                RadioChecked[i].setVisibility(Component.INVISIBLE);
            }
            int num = i;
            ItemLayouts[i].setComponentStateChangedListener((c, v) ->
            {
                if(v == 18432)
                    StatusPressedItems[num].setVisibility(Component.VISIBLE);
                else
                    StatusPressedItems[num].setVisibility(Component.INVISIBLE);
            });
            ItemLayouts[i].setClickedListener(component ->
            {
                itemValue = num;
                RadioEmpty[num].setVisibility(Component.HIDE);
                RadioChecked[num].setVisibility(Component.VISIBLE);
                for(int o = 0;o < mItems.length;o++)
                {
                    if(o!=num)
                    {
                        RadioEmpty[o].setVisibility(Component.VISIBLE);
                        RadioChecked[o].setVisibility(Component.HIDE);
                    }
                }
                sDialog.destroy();
                isDestroyed = true;
                System.gc();
            });
        }
        Button button = (Button)dialogLayout.findComponentById(ResourceTable.Id_CancelButton);
        button.setBackground(new GeneralButtonElement(mContext,GeneralButtonElement.TYPE_Empty_Button));
        button.setTextColor(new Color(ResourceTool.getColor(mContext, ResourceTable.Color_dawnlighti_button_accent, -417762)));
        button.setText(mCancel);
        button.setClickedListener(component -> { if (sDialog != null) {itemValue = -1;sDialog.destroy(); isDestroyed = true; System.gc();} });
        return dialogLayout;
    }

    public int getItemValue()
    {
        return itemValue;
    }

    public boolean isShowing()
    {
        return sDialog.isShowing();
    }

    public void destory()
    {
        sDialog.destroy();
        isDestroyed = true;
    }
}